package com.esm.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.omg.CORBA.Request;

import com.esm.dao.DepartmentCrodeDao_lxy;
import com.esm.model.Department_lxy;
import com.esm.model.DepartmentCrode_lxy;
import com.esm.model.User;
import com.esm.utils.JDBCUtil;
/**
 * 这是部门记录dao接口的实现
 * @author LiuXingYu
 *
 */
public class DepartmentCrodeImpl_lxy implements DepartmentCrodeDao_lxy{
	@Override
   public  int getAllDepartmentCrode(){
	    int flag = 0;
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT COUNT(rec_id) depcount ");
		sql.append(" FROM ems_deptrecorde");
		Connection con=JDBCUtil.getConnection();
		PreparedStatement pst = null;
		try {
			 pst=con.prepareStatement(sql.toString());
			ResultSet rs=pst.executeQuery();
			if(rs.next()){
				 flag=rs.getInt("depcount");
			}
		    } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		    }
		JDBCUtil.closeJDBC(pst, con);
		return flag;
   };
   @Override
   public List<DepartmentCrode_lxy> selectDeparmentCrodes(int page, int rows) {
		// TODO Auto-generated method stub
		List<DepartmentCrode_lxy> departmentCrodes=new ArrayList<DepartmentCrode_lxy>();
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT rec_id,rec_event,rec_dept,rec_man,time,runner FROM ems_deptrecorde ");
		sql.append(" limit ?,? ");
		Connection con=JDBCUtil.getConnection();
		PreparedStatement pst=null;
		try {
			 pst=con.prepareStatement(sql.toString());
			pst.setInt(1, (page-1)*rows);
			pst.setInt(2, rows);
			ResultSet rs= pst.executeQuery();
			while(rs.next()){
				DepartmentCrode_lxy depCro= new DepartmentCrode_lxy();
				depCro.setcId(rs.getInt("rec_id"));
				depCro.setcEvent(rs.getString("rec_event"));
				depCro.setcDept(rs.getString("rec_dept"));
				depCro.setcMan(rs.getString("rec_man"));
				depCro.setcTime(rs.getString("time"));
				depCro.setcRunner(rs.getString("runner"));
				departmentCrodes.add(depCro);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBCUtil.closeJDBC(pst, con);
		return departmentCrodes;
	}
   /**
    * 增加记录
    * @return
    * @author liuxingyu
    */
   public static boolean getAddDepartmentCrode(String deptName,String manager,String currentUname){
	   boolean  flag=false;
		StringBuffer sql=new StringBuffer();
		sql.append("INSERT INTO ems_deptrecorde (rec_event,rec_dept,rec_man,time,runner) VALUES ('增加部门',?,?,CURDATE(),?)");
		Connection con=JDBCUtil.getConnection();
		PreparedStatement pst=null;
		try {
			 pst=con.prepareStatement(sql.toString());
			 pst.setString(1, deptName);
			 pst.setString(2, manager);
			 pst.setString(3, currentUname);
			boolean rs=pst.execute();
			if(rs){
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBCUtil.closeJDBC(pst, con);
		return flag;
   };
   /**
    * 修改记录
    * @return
    * @author liuxingyu
    */
   public static boolean getUpdateDepartmentCrode(String deptName,String manager,String currentUname){
	   boolean  flag=false;
		StringBuffer sql=new StringBuffer();
		sql.append("INSERT INTO ems_deptrecorde (rec_event,rec_dept,rec_man,time,runner) VALUES ('修改部门',?,?,CURDATE(),?)");
		Connection con=JDBCUtil.getConnection();
		PreparedStatement pst=null;
		try {
			 pst=con.prepareStatement(sql.toString());
			 pst.setString(1, deptName);
			 pst.setString(2, manager);
			pst.setString(3, currentUname);
			boolean rs=pst.execute();
			if(rs){
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBCUtil.closeJDBC(pst, con);
		return flag;
   };
   /**
    * 删除记录
    * @return
    */
   public static boolean getDeleteDepartmentCrode(String deptName,String manager,String currentUser){
	   boolean  flag=false;
	  
		StringBuffer sql=new StringBuffer();
		sql.append("INSERT INTO ems_deptrecorde (rec_event,rec_dept,time,runner) VALUES ('删除部门',?,CURDATE(),?)");
		Connection con=JDBCUtil.getConnection();
		PreparedStatement pst=null;
		try {
			 pst=con.prepareStatement(sql.toString());
			 pst.setString(1, deptName);
			 pst.setString(2, manager);
			pst.setString(3, currentUser);
			boolean rs=pst.execute();
			if(rs){
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBCUtil.closeJDBC(pst, con);
		return flag;
   };
}
